package com.egao.api.controller.orderB;


import com.egao.api.base.BaseApiController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageResult2;
import com.egao.common.system.entity.OperRecord;
import com.egao.common.system.service.OperRecordService;
import com.egao.orderB.entity.Bill;
import com.egao.orderB.entity.BillFee;
import com.egao.orderB.service.BillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * https://francjc.com:4332/swagger-ui.html
 */
@RequestMapping("/api/bill")
@RestController
@Api(value = "订单",tags = {"订单"})
public class ApiBillController extends BaseApiController {

    @PostMapping("/list")
    @ApiOperation(value = "表格订单列表",notes = "limit:每页显示几行 必填\n" +
            "page:第几页 必填\n" +
//            "customer  客户名称\n" +
            "contacts  联系人\n" +
            "billOfLading  提单号\n" +
            "carNo  车牌\n" +
            "boxNum  箱号\n" +
            "factoryAddress  工厂地址\n" +
            "arrivalPort  进港码头\n" +
            "whiteCard  白卡号\n" +
            "state  状态\n" +
            "date1  做箱(pack_date)开始时间 比如2020-10-10 12:00:00" +
            "date2  做箱(pack_date)结束时间 比如2020-10-10 14:00:00\n" +
            " \n\n\n" +
            "返回的结果:\n" +
            "id int id\n" +
            "company_id varchar 公司id\n" +
            "customer varchar 客户名称\n" +
            "contacts varchar 联系人\n" +
            "contacts_phone varchar 联系电话\n" +
            "dock varchar 码头\n" +
            "province varchar 模糊地址\n" +
            "address varchar 详细地址\n" +
            "weight varchar 箱门重量\n" +
            "box_type varchar 箱型\n" +
            "price varchar 价格\n" +
            "pack_date varchar 装箱日期(客户要求的装箱时间)\n" +
            "vessel varchar 船名\n" +
            "voyage varchar 航次\n" +
            "whiteCard varchar 白卡号\n" +
            "bill_of_lading varchar 提单号\n" +
            "factory_address varchar 工厂地址\n" +
            "factory_phone varchar 装箱人电话\n" +
            "qr_code varchar 二维码\n" +
            "car_team varchar 车队\n" +
            "driver varchar 司机\n" +
            "driver_phone varchar 司机电话\n" +
            "car_no varchar 车牌\n" +
            "driver_freight varchar 运费\n" +
            "box_num varchar 箱号\n" +
            "box_num_pic varchar 箱号图片\n" +
            "sealno varchar 封号\n" +
            "sealno_pic varchar 封号图片\n" +
            "arrived_factory_latitude varchar 到厂纬度\n" +
            "arrived_factory_longitude varchar 到厂经度\n" +
            "arrived_factory_date varchar 到厂时间\n" +
            "arrived_factory_warn_date varchar 到厂预警时间(默认提前一小时)\n" +
            "leaved_factory_date varchar 离厂时间\n" +
            "leaved_factory_longitude varchar 离厂经度\n" +
            "leaved_factory_latitude varchar 离厂纬度\n" +
            "arrived_harbour_date varchar 到港时间\n" +
            "arrived_harbour_latitude varchar 到港纬度\n" +
            "arrived_harbour_longitude varchar 到港经度\n" +
            "arrived_harbour_warn_date varchar 到港预警时间(默认提前一小时)\n" +
            "annex 附件\n" +
            "state varchar 状态:下单,做单,提箱,已到厂,已离厂,已进港,发起费用清单,客服审核,客户确认,结单\n" +
            "offertime:下单时间\n" +
            "maketime:做单时间\n" +
            "driverreceivetime:司机接单时间\n" +
            "upboxtime:提箱时间\n" +
            "arrivedfactorytime:到厂时间\n" +
            "leavefactorytime:离场时间\n" +
            "inporttime:进港时间\n" +
            "costbilltime:发起费用清单时间\n" +
            "verifytime:货代费用审核时间\n" +
            "driverFeeVerifytime:司机费用审核时间\n" +
            "customerverifytime:客户确认时间\n" +
            "upYard:提箱堆场\n" +
            "backYard:还箱堆场\n" +
            "billFeeList:费用信息\n" +
            "billProcessList:费用流程\n" +
            "arrivalPort:进港码头\n" +
            "sfile:文件拖拽\n" +
            "remark varchar 备注\n" +
            "create_time datetime 创建时间\n" +
            "create_user varchar 创建人\n" +
            "update_time datetime 更新时间\n" +
            "update_user varchar 更新人\n",httpMethod = "POST")
    @ApiImplicitParam(name = "remark", value = "备注", dataType = "com.egao.orderB.entity.Bill")
    @ResponseBody
    public JSONObject list(PageResult2<T> page, Bill bill, HttpServletRequest request) throws Exception {
        return billService.list(page, bill, getLoginUser(request));
    }
    @Autowired
    private OperRecordService operRecordService;

    @Autowired
    private BillService billService;

    @PostMapping("/listAll")
    @ApiOperation(value = "订单列表",notes = "" +
            "customer  客户名称\n" +
            "contacts  联系人\n" +
            "billOfLading  提单号\n" +
            "carNo  车牌\n" +
            "boxNum  箱号\n" +
            "factoryAddress  工厂地址\n" +
            "arrivalPort  进港码头\n" +
            "whiteCard  白卡号\n" +
            "state  状态\n\n\n\n" +
            "司机当前可接单列表:当state为已做单,driverPhone为司机手机号为接单人\n" +
            //"司机当前已经接单列表:当state为已接单,driverPhone为司机手机号为接单人\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public List<Bill> listAll(Bill bill, HttpServletRequest request) throws Exception {
        return billService.list(bill, getLoginUser(request));
    }


    @PostMapping("/getById")
    @ApiOperation(value = "订单查询",notes = "id ",httpMethod = "POST")
    @ResponseBody
    public JsonResult getById(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "订单查询", "订单查询",
                request.getServletPath(), "POST", "ApiBillController.getById", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return JsonResult.ok().setData(billService.getById(bill));
    }

//    @PostMapping("/save")
//    @ApiOperation(value = "订单添加",notes = "id ",httpMethod = "POST")
//    @ResponseBody
//    public JsonResult save(Bill bill, HttpServletRequest request) {
//        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "订单添加", "订单修改",
//                request.getServletPath(), "POST", "ApiBillController.save", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
//                "", getIpAddress(request)));
//        return billService.save(bill, getLoginUser(request));
//    }

    @PostMapping("/offer")
    @ApiOperation(value = "客户下单 1",notes = "" +
            "customer:客户名称 必填\n" +
//            "contacts:联系人 必填\n" +
//            "contactsPhone:联系电话 必填\n" +
            "dock:码头A 必填\n" +
            "province:模糊的地址 必填\n" +
            "address:具体地址\n" +
            "weight:重量 必填\n" +
            "boxType:箱型 必填\n" +
            "price:报价 必填\n" +
            "packDate:装箱日期(客户要求的装箱时间) 必填\n" +
            "vessel:船名 必填\n" +
            "voyage:航次 必填\n" +
            "billOfLading:提单号 必填\n" +
            "factoryAddress:工厂地址 必填\n" +
            "factoryPhone:装箱人电话\n" +
            "arrivedFactoryWarnDate varchar 到厂预警时间(默认提前二小时) 必填\n" +
            "arrivedHarbourWarnDate varchar 到港预警时间(默认提前二小时) 必填\n" +
            "annex:附件\n" +
            "qrCode:二维码\n" +
            "remark:备注\n" +
            "sfile:文件拖拽\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult offer(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户下单", "客户下单",
                request.getServletPath(), "POST", "ApiBillController.offer", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.order(bill, getLoginUser(request));
    }

    @PostMapping("/make")
    @ApiOperation(value = "做单 2",notes = "" +
            "id \n" +
            "carTeam varchar 车队\n" +
            "carNo varchar 车牌\n" +
            "driver varchar 司机 必填\n" +
            "driverPhone varchar 司机电话\n" +
            "driverFreight varchar 运费 必填\n" +
            "whiteCard varchar 白卡号 \n" +
            "remark:备注\n" +
            "sfile:文件拖拽\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult make(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "做单", "做单",
                request.getServletPath(), "POST", "ApiBillController.make", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.make(bill, getLoginUser(request));
    }

    @PostMapping("/driverReceive")
    @ApiOperation(value = "司机接单 3",notes = "" +
            "id \n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult driverReceive(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "做单", "做单",
                request.getServletPath(), "POST", "ApiBillController.make", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.driverReceive(bill, getLoginUser(request));
    }

    @PostMapping("/upBox")
    @ApiOperation(value = "提箱 4",notes = "" +
            "id \n" +
            "boxNum varchar 箱号 必填\n" +
            "boxNumPic varchar 箱号图片 必填\n" +
            "upYard varchar 提箱堆场\n" +
            "sealno varchar 封号 必填\n" +
            "sealnoPic varchar 封号图片 必填\n" +
            "arrivalPort:进港码头\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult upBox(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "已派车", "已派车",
                request.getServletPath(), "POST", "ApiBillController.upBox", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.upBox(bill, getLoginUser(request));
    }

    @PostMapping("/arrivedFactory")
    @ApiOperation(value = "到厂 5",notes = "" +
            "id \n" +
            "arrivedFactoryLatitude varchar 到厂纬度 必填\n" +
            "arrivedFactoryLongitude varchar 到厂经度 必填\n" +
            "arrivedFactoryDate varchar 到厂时间 必填\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult arrivedFactory(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "到厂", "到厂",
                request.getServletPath(), "POST", "ApiBillController.arrivedFactory", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.arrivedFactory(bill, getLoginUser(request));
    }
    @PostMapping("/leaveFactory")
    @ApiOperation(value = "离厂 6",notes = "" +
            "id \n" +
            "leavedFactoryDate varchar 离厂时间 必填\n" +
            "leavedFactoryLongitude varchar 离厂经度 必填\n" +
            "leavedFactoryLatitude varchar 离厂纬度 必填\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult leaveFactory(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "离厂", "离厂",
                request.getServletPath(), "POST", "ApiBillController.leaveFactory", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.leaveFactory(bill, getLoginUser(request));
    }

    @PostMapping("/inPort")
    @ApiOperation(value = "进港  7",notes = "" +
            "id \n" +
            "arrivedHarbourDate varchar 到港时间 必填\n" +
            "arrivedHarbourLatitude varchar 到港纬度 必填\n" +
            "arrivedHarbourLongitude varchar 到港经度 必填\n" +
            "preEntryPic varchar 预录入照片 必填\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult inPort(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "进港", "进港",
                request.getServletPath(), "POST", "ApiBillController.inPort", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.inPort(bill, getLoginUser(request));
    }

    @PostMapping("/costBill")
    @ApiOperation(value = "发起费用清单 8",notes = "" +
            "id \n" +
            "remark:备注\n" +
            "\n" +
            "例子:{\"id\":\"145\",\"remark\":\"6666\",\"billFeeList\":[{\"billId\":145, \"type\":\"类型3\",\"boxType\":\"箱型\", \"feeName\":\"费用名称3\", \"price\":\"金额3\", \"remark\":\"备注3\"},{\"billId\":145, \"type\":\"类型4\",\"boxType\":\"箱型\", \"feeName\":\"费用名称4\", \"price\":\"金额4\", \"remark\":\"备注4\"}]}\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult costBill(@RequestBody Bill bill, HttpServletRequest request)throws Exception {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "发起费用清单", "发起费用清单",
                request.getServletPath(), "POST", "ApiBillController.inPort", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        try {
            return billService.costBill(bill, getLoginUser(request));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("请填写订单id,操作失败!");
        }
    }

    @PostMapping("/driverFeeVerify")
    @ApiOperation(value = "司机费用审核 9并列",notes = "" +
            "id \n" +
            "remark:备注\n" +
            "\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult driverFeeVerify(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "司机费用审核", "司机费用审核",
                request.getServletPath(), "POST", "ApiBillController.司机费用审核", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.driverFeeVerify(bill, getLoginUser(request));
    }
    @PostMapping("/verify")
    @ApiOperation(value = "货代费用审核 9并列",notes = "" +
            "id \n" +
            "remark:备注\n" +
            "\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult verify(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客服审核", "客服审核",
                request.getServletPath(), "POST", "ApiBillController.verify", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.verify(bill, getLoginUser(request));
    }

    @PostMapping("/customerVerify")
    @ApiOperation(value = "客户确认 10",notes = "" +
            "id \n" +
            "remark:备注\n" +
            "\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult customerVerify(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户确认", "客户确认",
                request.getServletPath(), "POST", "ApiBillController.customerVerify", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.customerVerify(bill, getLoginUser(request));
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改订单",notes = "" +
            "id:id 必填\n" +
            "customer:客户名称 必填\n" +
            "contacts:联系人 必填\n" +
            "contactsPhone:联系电话 必填\n" +
            "dock:码头 必填\n" +
            "province:模糊地址  必填\n" +
            "address:详细地址\n" +
            "weight:箱门重量 必填\n" +
            "boxType:箱型 必填\n" +
            "price:价格 必填\n" +
            "packDate:装箱日期(客户要求的装箱时间) 必填\n" +
            "vessel:船名 必填\n" +
            "voyage:航次 必填\n" +
            "voyage:航次  \n" +
            "billOfLading:提单号 必填\n" +
            "factoryAddress:工厂地址 必填\n" +
            "factoryPhone:装箱人电话\n" +
            "qrCode:二维码\n" +
            "carTeam:车队\n" +
            "driver:司机\n" +
            "driverPhone:司机电话\n" +
            "carNo:车牌\n" +
            "driver_freight:运费\n" +
            "boxNum:箱号\n" +
            "boxNumPic:箱号图片\n" +
            "sealno:封号\n" +
            "sealnoPic:封号图片\n" +
            "arrivedFactoryLatitude:到厂纬度\n" +
            "arrivedFactoryLongitude:到厂经度\n" +
            "arrivedFactoryDate:到厂时间\n" +
            "arrivedFactoryWarnDate:到厂预警时间(默认提前一小时)\n" +
            "leavedFactoryDate:离厂时间\n" +
            "leavedFactoryLongitude:离厂经度\n" +
            "leavedFactoryLatitude:离厂纬度\n" +
            "arrivedHarbourDate:到港时间\n" +
            "arrivedHarbourLatitude:到港纬度\n" +
            "arrivedHarbourLongitude:到港经度\n" +
            "arrivedHarbourWarnDate:到港预警时间(默认提前一小时)\n" +
            "preEntryPic:预录入照片\n" +
            "upYard:提箱堆场\n" +
            "backYard:还箱堆场\n" +
            "arrivalPort:进港码头\n" +
            "sfile:文件拖拽\n" +
            "whiteCard:白卡号\n" +
            "annex:附件\n" +
            "remark:备注" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult update(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "修改订单", "修改订单",
                request.getServletPath(), "POST", "ApiBillController.update", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.update(bill, getLoginUser(request));
    }

    @PostMapping("/billCancel")
    @ApiOperation(value = "取消订单",notes = "" +
            "id \n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult billCancel(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "订单取消", "订单取消",
                request.getServletPath(), "POST", "ApiBillController.billCancel", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.billCancel(bill, getLoginUser(request));
    }

    @PostMapping("/receivable")
    @ApiOperation(value = "应收确认",notes = "" +
            "id bill表id\n" +
            "remark 备注 \n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult receivable(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "应收确认", "应收确认",
                request.getServletPath(), "POST", "ApiBillController.receivable", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.receivable(bill, getLoginUser(request));
    }

    @PostMapping("/payable")
    @ApiOperation(value = "应付确认",notes = "" +
            "id bill表id\n" +
            "remark 备注 \n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult payable(Bill bill, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "应付确认", "应付确认",
                request.getServletPath(), "POST", "ApiBillController.payable", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));

        return billService.payable(bill, getLoginUser(request));
    }

}
